/**
 * 掉落盒子脚本，实现盒子碰撞及回收流程
 */
export default class Box1 extends Laya.Script {
    /**刚体对象引用 */
    private _rig: Laya.RigidBody;

     /** @prop {name:nextLevelBox,tips:"掉落容器预制体对象1",type:Prefab}*/
     nextLevelBox: Laya.Prefab;
     private _nextLevelBox: Laya.Sprite;

    constructor() { super(); }

    onEnable(): void {
        /**获得组件引用，避免每次获取组件带来不必要的查询开销 */
        this._rig = this.owner.getComponent(Laya.RigidBody);
    }

    onUpdate(): void {
       
    }

    onTriggerEnter(other: any, self: any, contact: any): void {
        var owner: Laya.Sprite = this.owner as Laya.Sprite;
        if (other.label === "box1") {
            Laya.SoundManager.playSound("sound/hit.wav");
            if (owner.parent) {
                let effect: Laya.Animation = Laya.Pool.getItemByCreateFun("effect", this.createEffect, this);
                effect.pos(owner.x, owner.y);
                owner.parent.addChild(effect);
                effect.play(0, true);
                owner.removeSelf();
                Laya.SoundManager.playSound("sound/destroy.wav");
                }
            }
    }

    /**使用对象池创建爆炸动画 */
    createEffect(): Laya.Animation {
        let ani: Laya.Animation = new Laya.Animation();
        ani.loadAnimation("test/TestAni.ani");
        ani.on(Laya.Event.COMPLETE, null, recover);
        function recover(): void {
            ani.removeSelf();
            Laya.Pool.recover("effect", ani);
        }
        return ani;
    }

    createNextLevelBox():Laya.Sprite {
        let nextLevelBox: Laya.Prefab = 
        let _nextLevelBox: Laya.Sprite = 
        return
    }

    onDisable(): void {
        //盒子被移除时，回收盒子到对象池，方便下次复用，减少对象创建开销。
        Laya.Pool.recover("box1", this.owner);
    }
}